package com.cyt.web.controller.work.antiepidemic;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.antiepidemic.model.po.AntiepidemicBuilding;
import com.cyt.work.antiepidemic.model.po.AntiepidemicCommunity;
import com.cyt.work.antiepidemic.model.po.AntiepidemicPerson;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicPersonExportVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicPersonVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicRecordExportVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicRecordVo;
import com.cyt.work.antiepidemic.service.AntiepidemicBuildingService;
import com.cyt.work.antiepidemic.service.AntiepidemicCommunityService;
import com.cyt.work.antiepidemic.service.AntiepidemicPersonService;
import com.cyt.work.antiepidemic.service.AntiepidemicRecordService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * e门通-社区出入信息控制层
 *
 * @author rong yuan
 * @date 2020-2-12 2:24:31
 */
@RestController
public class AntiepidemicRecordController extends BaseController {

    @Resource
    private AntiepidemicRecordService antiepidemicRecordService;
    @Resource
    private AntiepidemicPersonService antiepidemicPersonService;
    @Resource
    private AntiepidemicCommunityService antiepidemicCommunityService;
    @Resource
    private AntiepidemicBuildingService antiepidemicBuildingService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @RequestMapping("/work/antiepidemic/antiepidemicrecord/*")
    public Model goPage(Model model) {
        return model;
    }

    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/getAntiepidemicRecordList", method = RequestMethod.GET)
    public ModelAndView getDictContent(String personId) {
        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        // 需要根据社区判断
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());
        ModelAndView view;
        if (StringUtils.equals(antiepidemicCommunity.getCommunityType(), "2")) {
            view = new ModelAndView("/work/antiepidemic/antiepidemicrecord/antiepidemicRecordListT");
        } else {
            view = new ModelAndView("/work/antiepidemic/antiepidemicrecord/antiepidemicRecordList");
        }
        view.addObject("personId", personId);
        view.addObject("antiepidemicPerson", antiepidemicPerson);
        return view;
    }

    /**
     * 分页查询
     *
     * @param antiepidemicRecordVo 接受参数对象
     * @param page                 当前页面
     * @param limit                页面数量
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/page", method = RequestMethod.POST)
    public ReturnResult page(AntiepidemicRecordVo antiepidemicRecordVo, int page, int limit) {
        return antiepidemicRecordService.page(antiepidemicRecordVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/work/antiepidemic/antiepidemicrecord/antiepidemicRecordEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(antiepidemicRecordService.selectById(id));
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request              请求对象
     * @param antiepidemicRecordVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, AntiepidemicRecordVo antiepidemicRecordVo) {
        return antiepidemicRecordService.addOrUpdate(antiepidemicRecordVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request              请求对象
     * @param antiepidemicRecordVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, AntiepidemicRecordVo antiepidemicRecordVo) {
        return antiepidemicRecordService.remove(antiepidemicRecordVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request              请求对象
     * @param antiepidemicRecordVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, AntiepidemicRecordVo antiepidemicRecordVo) {
        return antiepidemicRecordService.delete(antiepidemicRecordVo, acceptCurrentUser(request));
    }

    @RequestMapping(value = "/work/antiepidemic/antiepidemicrecord/exportExcel")
    public void exportExcel(HttpServletResponse response, AntiepidemicRecordVo antiepidemicRecordVo) {
        List<AntiepidemicRecordExportVo> list = antiepidemicRecordService.selectExportList(antiepidemicRecordVo);
        // 导出操作
        EasyPoiUtil.exportExcel(list, "出入小区记录", "出入小区记录", AntiepidemicRecordExportVo.class, "出入小区记录.xls", response);
    }
}